package com.fry.mall.wallet.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 钱包的拦截器
 *
 * @Author: FRY
 * @CreateTime: 2023-03-02  21:52
 */
public class WalletInterceptor implements HandlerInterceptor {

    public static ThreadLocal<String> threadLocal =  new ThreadLocal<>();

    /**
     * 假设用户登陆时将信息存入session，进入钱包服务后，从session中取出登陆用户ID
     * 将登陆的用户ID存入本地线程中
     * @author: FRY
     * @date: 2023/03/02 21:54
     * @param: request
     * @param: response
     * @param: handler
     * @return: boolean
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("loginAccount");

        threadLocal.set(userId);

        return true;
    }
}
